const connection = require("../app/database");
const dayjs = require("dayjs");
const { v4: uuidv4 } = require("uuid");

class UserService {
  constructor() {
    this.formName = "user";
  }
  // 新增
  async create(_user) {
    return new Promise(async (resolve, reject) => {
      try {
        const { name, password, telPhone, avatar } = _user;
        const id = uuidv4();
        const createTime = dayjs().format("YYYY-MM-DD HH:mm:ss");
        const statement = `INSERT INTO ${this.formName} (id,name,password,telPhone,avatar,createTime,updateTime) VALUES (?,?,?,?,?,?,?);`;
        const res = await connection.execute(statement, [id, name, password, telPhone, avatar, createTime, createTime]);
        resolve(res);
      } catch (error) {
        reject(error);
      }
    });
  }
  // 批量删除
  async delete(_ids) {
    return new Promise(async (resolve, reject) => {
      try {
        const placeholders = _ids.map(() => "?").join(",");
        console.log(placeholders);

        const statement = `DELETE FROM ${this.formName} WHERE id IN (${placeholders});`;
        console.log(statement);
        const [res] = await connection.execute(statement, _ids);
        resolve(res);
      } catch (error) {
        reject(error);
      }
    });
  }
  // 分页搜索
  async page(_search) {
    return new Promise(async (resolve, reject) => {
      const { pageNum, pageSize, name = "", telPhone = "" } = _search;

      const offset = (pageNum - 1) * pageSize;
      const limit = pageSize;
      try {
        const statement = `
          SELECT * FROM ${this.formName}
          WHERE name LIKE ? AND telPhone LIKE ?
          LIMIT ${offset}, ${limit};
        `;
        const [res] = await connection.execute(statement, [`%${name}%`, `%${telPhone}%`]);
        resolve(res);
      } catch (error) {
        reject(error);
      }
    });
  }
  // 编辑用户
  async update(_detail) {
    const { id, name, telPhone, avatar } = _detail;
    const updateTime = dayjs().format("YYYY-MM-DD HH:mm:ss");
    return new Promise(async (resolve, reject) => {
      try {
        const statement = `UPDATE ${this.formName} SET name = ?, telPhone = ?, avatar = ?, updateTime = ? WHERE id = ?;`;
        const res = await connection.execute(statement, [name, telPhone, avatar, updateTime, id]);
        resolve(res);
      } catch (error) {
        reject(error);
      }
    });
  }
  // 查询用户
  async findName(_name) {
    return new Promise(async (resolve, reject) => {
      try {
        const statement = `SELECT * FROM ${this.formName} WHERE name = ?;`;
        const [res] = await connection.execute(statement, [_name]);
        resolve(res);
      } catch (error) {
        reject(error);
      }
    });
  }
  // 详情
  async detail(_id) {
    return new Promise(async (resolve, reject) => {
      try {
        const statement = `SELECT * FROM ${this.formName} WHERE id = ?;`;
        const [res] = await connection.execute(statement, [_id]);
        resolve(res[0]);
      } catch (error) {
        reject(error);
      }
    });
  }

  // 总数
  async allNum() {
    return new Promise(async (resolve, reject) => {
      try {
        const statement = `SELECT COUNT(*) FROM ${this.formName};`;
        const [res] = await connection.execute(statement, []);
        resolve(res[0]["COUNT(*)"]);
      } catch (error) {
        reject(error);
      }
    });
  }

  // 修改密码
  async resetpassword(_params) {
    return new Promise(async (resolve, reject) => {
      const { id, password } = _params;
      try {
        const statement = `UPDATE ${this.formName} SET password = ? WHERE id = ?;`;
        const [res] = await connection.execute(statement, [password, id]);
        resolve(res);
      } catch (error) {
        reject(error);
      }
    });
  }
}

module.exports = new UserService();
